package vip.xumy.admin.bmap.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import lombok.extern.log4j.Log4j2;

/**
 * @author:mengyxu
 * @date:2020年4月16日
 */

@Log4j2
@Controller
@RequestMapping("image")
public class ImageController {
	@Value("${image.bmap.maptile}")
	private String filePath;

	@RequestMapping(value = "tile/{b}/{d}/{a}", produces = MediaType.IMAGE_PNG_VALUE)
	@ResponseBody
	public byte[] getImage(@PathVariable("b") String b, @PathVariable("d") String d, @PathVariable("a") String a)
			throws IOException {
		String path = filePath + "/" + b + "/" + d + "/" + a;
		File file = new File(path);
		if (!file.exists()) {
			log.warn("找不到目标文件：" + path);
			file = new File(filePath, "0.png");
		}
		try (FileInputStream input = new FileInputStream(file);) {
			byte[] bytes = new byte[input.available()];
			input.read(bytes);
			return bytes;
		}
	}

}
